<?php

class wikiRetriever{

	var $baseURL;
	var $ch;
	
	function __construct($limit=100){
		
		$this->baseURL = "http://de.wikipedia.org/w/api.php?";

		define("EMAIL_ADDRESS", "me@hotmail.com");
		$this->ch = curl_init();
		$this->cv = curl_version();
		$user_agent = "curl ${cv['version']} (${cv['host']}) libcurl/${cv['version']} ${cv['ssl_version']} zlib/${cv['libz_version']} <" . EMAIL_ADDRESS . ">"; 
		curl_setopt($this->ch, CURLOPT_USERAGENT, $user_agent);
		curl_setopt($this->ch, CURLOPT_COOKIEFILE, "cookies.txt");
		curl_setopt($this->ch, CURLOPT_COOKIEJAR, "cookies.txt");
		curl_setopt($this->ch, CURLOPT_ENCODING, "deflate, gzip, identity");
		curl_setopt($this->ch, CURLOPT_HEADER, FALSE);
		curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($this->ch, CURLOPT_HTTPGET, TRUE);
		curl_setopt($this->ch, CURLOPT_URL, $this->baseURL."action=query&generator=allpages&gaplimit=".$limit."&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content&format=xml"); 
		$xml = curl_exec($this->ch);
		$this->xml_reader = new XMLReader();
		$this->xml_reader->xml($xml, "UTF-8");
		
		
		$latest_rev = array();
		while ($this->xml_reader->read()) {
		    if ($this->xml_reader->nodeType == XMLReader::ELEMENT) {
		        if ($this->xml_reader->name == "page") {
		            $this->latest_rev[$this->xml_reader->getAttribute("title")] = preg_replace('/\s+/'," ",strip_tags($this->extract_first_rev()));
		        }
		    }
		}
		
		
		// foreach ($latest_rev as $title => $latest_rev) {
		//     $this->ret[]  = preg_replace('/\s+/'," ",strip_tags($this->parse($latest_rev)));
		// }
		
	}
	
	function get(){
		return implode("\n",$this->latest_rev);
	}

	function extract_first_rev() {
    	while ($this->xml_reader->read()) {
        	if ($this->xml_reader->nodeType == XMLReader::ELEMENT) {
            	if ($this->xml_reader->name == "rev") {
                	$content = htmlspecialchars_decode($this->xml_reader->readInnerXML(), ENT_QUOTES);
                	return $content;
            	}
        	} else if ($this->xml_reader->nodeType == XMLReader::END_ELEMENT) {
            	if ($this->xml_reader->name == "page") {
                	throw new Exception("Unexpectedly found `</page>`");
            	}
        	}
    	}
    	throw new Exception("Reached the end of the XML document without finding revision content");
	}

	function parse($rev) {
    	
    	curl_setopt($this->ch, CURLOPT_HTTPGET, TRUE);
    	curl_setopt($this->ch, CURLOPT_URL, $this->baseURL."action=parse&text=".rawurlencode($rev)."&prop=text&format=xml");
    	sleep(3);
    	$xml = curl_exec($this->ch);
		echo $this->baseURL."action=parse&text=".rawurlencode($rev)."&prop=text&format=xml";
    	// $xml_reader = new XMLReader();
    	// $xml_reader->xml($xml, "UTF-8");
    	// 
    	// 	    while ($xml_reader->read()) {
    	// 	        if ($xml_reader->nodeType == XMLReader::ELEMENT) {
    	// 	            if ($xml_reader->name == "text") {
    	// 	                $html = htmlspecialchars_decode($xml_reader->readInnerXML(), ENT_QUOTES);
    	// 	                return $html;
    	// 	            }
    	// 	        }
    	// 	    }
    	// 
    	// throw new Exception("Failed to parse");
	}
}